package blyth;

/*
 * Class Tile
 * The agent's static beliefs about a simple Tile
 * of the world.
 */
public class Tile {

    public final static int basicMovementCost = 5;
    public final static double waterCostMultiplier = 30.0;
    // Is this tile visited yet?
    private boolean isVisited;
    // The hassle related to being close to edge
    private int edgeHassle;
    // The hassle related to being close to water (is set from outside)
    private int waterHassle;
    // Chance of water being here
    private double waterProbability;
    // Used at water prediction & some energy calcultaions
    public boolean isWater;
    // Hostile agent was seen here in this turn
    public int enemyWasHere;
    // The last hostile agent we saw here
    public Agent lastEnemySeenHere;
    // This tile was seen last time, in this turn
    public int lastCheck;

    public Tile(int edgeHassle) {
        this.edgeHassle = edgeHassle;
        waterHassle = 0;
        waterProbability = 0.0;
        isVisited = false;
        isWater = false;
        enemyWasHere = -1;
        lastEnemySeenHere = null;
        lastCheck = 0;
    }

    public void Visit(boolean inWater) {
        if (isVisited == false) {
            isVisited = true;
            if (inWater) {
                waterProbability = 1.0;
                isWater = true;
            } else {
                waterProbability = 0.0;
            }
        }
    }

    public void addWaterProbability(double waterProbability) {
        if (isVisited == false) {
            this.waterProbability += waterProbability;
            if (this.waterProbability > 1.0) {
                this.waterProbability = 1.0;
            }
        }
    }

    public boolean isVisited() {
        return isVisited;
    }

    public void setWaterHassle(int distance) {
        if (distance == 0) {
            waterHassle = 10;            
        } else if (distance == 1 && waterHassle < 5) {
            waterHassle = 5;            
        }        
    }

    public int getStaticHassle() {
        return edgeHassle + waterHassle;
    }

    public int getMovementCost() {
        if (waterProbability == 0.0) {
            return basicMovementCost;
        }
        return basicMovementCost + (int) Math.round(
                (double) basicMovementCost * ((waterCostMultiplier - 1.0) * waterProbability));
    }
    
    // Should be called, when we see this tile
    public void Refresh(Agent enemy) {
        lastEnemySeenHere = enemy;
        lastCheck = World.turn;
        if (enemy != null) {
            enemyWasHere = World.turn;
        }
    }
}